package cn.demo.um.service.impl;

import java.util.List;

import cn.demo.common.constant.CommonConstants;
import cn.demo.common.utils.bean.BeanUtils;
import com.mybatisflex.core.paginate.Page;
import cn.demo.common.core.page.PageQuery;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import cn.demo.um.mapper.UmAttrConfMapper;
import cn.demo.um.domain.UmAttrConf;
import cn.demo.um.service.IUmAttrConfService;

/**
 * 用户属性配置Service业务层处理
 *
 * @author xtjun
 * @date 2024-06-06
 */
@Service
@CacheConfig(cacheNames = "UmAttrConf", keyGenerator = "myKeyGenerator")
public class UmAttrConfServiceImpl extends ServiceImpl<UmAttrConfMapper, UmAttrConf> implements IUmAttrConfService {
    @Resource
    private UmAttrConfMapper umAttrConfMapper;
    /**
     * 查询用户属性配置
     *
     * @param id 用户属性配置主键
     * @return 用户属性配置
     */
    @Override
    @Cacheable(unless="#result == null")
    public UmAttrConf selectUmAttrConfById(String id) {
        return umAttrConfMapper.selectOneById(id);
    }

    /**
     * 查询用户属性配置列表
     *
     * @param umAttrConf 用户属性配置
     * @return 用户属性配置
     */
    @Override
    public Page<UmAttrConf> selectUmAttrConfPage(UmAttrConf umAttrConf, PageQuery pageQuery) {
        return umAttrConfMapper.paginate(pageQuery.build(), QueryWrapper.create(umAttrConf, UmAttrConf.operators).orderBy(UmAttrConf::getSort, true));
    }
    @Override
    public List<UmAttrConf> selectUmAttrConfList(UmAttrConf umAttrConf) {
        return umAttrConfMapper.selectListByQuery(QueryWrapper.create(umAttrConf, UmAttrConf.operators).orderBy(UmAttrConf::getSort, true));
    }
    @Override
    @Cacheable(unless="#result == null")
    public List<UmAttrConf> selectUmAttrConfAll() {
        List<UmAttrConf> confs = umAttrConfMapper.selectListByQuery(QueryWrapper.create().orderBy(UmAttrConf::getSort, true));
        int size = confs.size();
        for (int i = 0; i < size; i++) {
            UmAttrConf conf = confs.get(i);
            if (CommonConstants.YES.equals(conf.getIsStandard())){
                UmAttrConf copy = BeanUtils.copy(conf, UmAttrConf.class);
                copy.setAttrType(UmAttrConf.CONDITION_TYPE);
                copy.setAttrCode(UmAttrConf.CONDITION_PREFIX + conf.getAttrCode());
                confs.add(copy);
            }
        }
        return confs;
    }

    /**
     * 新增用户属性配置
     *
     * @param umAttrConf 用户属性配置
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true)
    public int insertUmAttrConf(UmAttrConf umAttrConf) {
        return umAttrConfMapper.insert(umAttrConf);
    }

    /**
     * 修改用户属性配置
     *
     * @param umAttrConf 用户属性配置
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true)
    public int updateUmAttrConf(UmAttrConf umAttrConf) {
        return umAttrConfMapper.update(umAttrConf);
    }

    /**
     * 批量删除用户属性配置
     *
     * @param ids 需要删除的用户属性配置主键
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true)
    public int deleteUmAttrConfByIds(List<String> ids) {
        return umAttrConfMapper.deleteBatchByIds(ids);
    }

    /**
     * 删除用户属性配置信息
     *
     * @param id 用户属性配置主键
     * @return 结果
     */
    @Override
    @CacheEvict(allEntries = true)
    public int deleteUmAttrConfById(String id) {
        return umAttrConfMapper.deleteById(id);
    }
}
